/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.StudentWitnessLike;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.StudentWitnessLikeRepository;
import me.zhengjie.modules.system.service.StudentWitnessLikeService;
import me.zhengjie.modules.system.service.dto.StudentWitnessLikeDto;
import me.zhengjie.modules.system.service.dto.StudentWitnessLikeQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.StudentWitnessLikeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2023-10-29
**/
@Service
@RequiredArgsConstructor
public class StudentWitnessLikeServiceImpl implements StudentWitnessLikeService {

    private final StudentWitnessLikeRepository studentWitnessLikeRepository;
    private final StudentWitnessLikeMapper studentWitnessLikeMapper;

    @Override
    public Map<String,Object> queryAll(StudentWitnessLikeQueryCriteria criteria, Pageable pageable){
        Page<StudentWitnessLike> page = studentWitnessLikeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(studentWitnessLikeMapper::toDto));
    }

    @Override
    public List<StudentWitnessLikeDto> queryAll(StudentWitnessLikeQueryCriteria criteria){
        return studentWitnessLikeMapper.toDto(studentWitnessLikeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StudentWitnessLikeDto findById(Long likeId) {
        StudentWitnessLike studentWitnessLike = studentWitnessLikeRepository.findById(likeId).orElseGet(StudentWitnessLike::new);
        ValidationUtil.isNull(studentWitnessLike.getLikeId(),"StudentWitnessLike","likeId",likeId);
        return studentWitnessLikeMapper.toDto(studentWitnessLike);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StudentWitnessLikeDto create(StudentWitnessLike resources) {
        return studentWitnessLikeMapper.toDto(studentWitnessLikeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(StudentWitnessLike resources) {
        StudentWitnessLike studentWitnessLike = studentWitnessLikeRepository.findById(resources.getLikeId()).orElseGet(StudentWitnessLike::new);
        ValidationUtil.isNull( studentWitnessLike.getLikeId(),"StudentWitnessLike","id",resources.getLikeId());
        studentWitnessLike.copy(resources);
        studentWitnessLikeRepository.save(studentWitnessLike);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long likeId : ids) {
            studentWitnessLikeRepository.deleteById(likeId);
        }
    }

    @Override
    public void download(List<StudentWitnessLikeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StudentWitnessLikeDto studentWitnessLike : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" witnessId",  studentWitnessLike.getWitnessId());
            map.put(" createTime",  studentWitnessLike.getCreateTime());
            map.put(" openId",  studentWitnessLike.getOpenId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}